package com.cyan.app.workflow.activity.WHM;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.cyan.app.barcode.CaptureActivity;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseFragmentActivity;
import com.cyan.app.workflow.bean.BaseBean;
import com.cyan.app.workflow.bean.InvbalBean;
import com.cyan.app.workflow.bean.ProgramLlLabBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.ClickUtil;
import com.cyan.app.workflow.util.network.CommHttpListener;
import com.cyan.app.workflow.util.network.VolleyUtil;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by DXW on 2016-04-15.
 * 程序发料-发料
 */
public class CxllLlActivity extends BaseFragmentActivity {
    public static final String TAG = "CxllLlActivity";
    private EditText mEditTextAppnum;
    private Button mButtonAppnum;
    private EditText mEditTextOnhand1;
    private Button mButtonOnhand1;
    private EditText mEditTextOnhand2;
    private EditText mEditTextUser;
    private EditText mEditTextFixnr;
    private Button mButtonUser;
    private Button mButtonFixnr;
    private Button mButtonSubmit;
    private LinearLayout mLinearLayoutUser;
    private ProgramLlLabBean.ProgramLlBean manpahdTBean;
    private ProgramLlLabBean.ProgramLlBean manpahdTBeanNew=null;
    private InvbalBean mInvbalBean=null;
    private static final String CxllLlYGBH = "cxll_ll_ygbh";
    private static final String CxllLlFIXNR= "cxll_ll_fixnr";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.whm_cxll_ll);
        titleBar();
        initData();
        //程序明细
        mButtonAppnum = (Button) findViewById(R.id.whm_cxll_ll_appnum_d);
        mButtonAppnum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("cxllPrograms", manpahdTBean);
                Intent intent = new Intent();
                intent.setClass(CxllLlActivity.this, CxllLlProgramsActivity.class);
                intent.putExtras(bundle);
                startActivityForResult(intent, 0x0001);
            }
        });
        //库位明细
        mButtonOnhand1 = (Button) findViewById(R.id.whm_cxll_ll_onhand1_u);
        mButtonOnhand1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(CxllLlActivity.this, GdllLlInvbatActivity.class);
                intent.putExtra("gdllLlItnbr", manpahdTBean.getItnbr());
                intent.putExtra("Appnum", Double.parseDouble(mEditTextAppnum.getText().toString()));
                intent.putExtra("Unmsr1", manpahdTBean.getUnmsr1());
                intent.putExtra("Unmsr2", manpahdTBean.getUnmsr2());
                intent.putExtra("Wareh", "W-R");
                startActivityForResult(intent, 0x0002);
            }
        });
        //发料人
        mButtonUser = (Button) findViewById(R.id.whm_cxll_ll_pisuser_sm);
        mButtonUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(CxllLlActivity.this, CaptureActivity.class), 0x0003);
            }
        });
        //目标库位
        mButtonFixnr = (Button) findViewById(R.id.whm_cxll_ll_fixnr_sm);
        mButtonFixnr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(CxllLlActivity.this, CaptureActivity.class), 0x0004);
            }
        });
        //工单明细
        mButtonSubmit = (Button) findViewById(R.id.whm_cxll_ll_submit);
        mButtonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ClickUtil.isFastClick(20)){
                    //20秒内不能重复操作
                    return;
                }
                if(manpahdTBeanNew.getAppNum()<=0){
                    AppUtils.showTip(CxllLlActivity.this, "申请数量必须大于0");
                }else if(mInvbalBean==null||mInvbalBean.getOnhand1()<=0){
                    AppUtils.showTip(CxllLlActivity.this, "发料数量必须大于0");
                }else if(mEditTextUser.getText().toString().isEmpty()){
                    AppUtils.showTip(CxllLlActivity.this, "发料人不能为空");
                }/*else if(mEditTextFixnr.getText().toString().isEmpty()){
                    AppUtils.showTip(CxllLlActivity.this, "目标库位不能为空");
                }*/else {
                    execLL();
                }
            }
        });
        //清空发料人
        mLinearLayoutUser = (LinearLayout) findViewById(R.id.whm_cxll_ll_user_clear);
        mLinearLayoutUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditTextUser.setText("");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x0001 && resultCode == RESULT_OK) {
            if(data != null) {
                manpahdTBeanNew = (ProgramLlLabBean.ProgramLlBean)data.getSerializableExtra("cxllProgramDetail");
                mEditTextAppnum.setText(String.valueOf(manpahdTBeanNew.getAppNum()));
            }

        }
        if(requestCode == 0x0002 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                mInvbalBean = (InvbalBean)data.getSerializableExtra("gdllInvbalDetail");
                mEditTextOnhand1.setText(String.valueOf(mInvbalBean.getOnhand1()));
                mEditTextOnhand2.setText(String.valueOf(mInvbalBean.getOnhand2()));
            }
        }
        if(requestCode == 0x0003 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("40010")) {
                AppUtils.showTip(CxllLlActivity.this, "员工不存在");
            }else {
                String pisUser = mEditTextUser.getText().toString();
                if(pisUser.contains(result.substring(5)))
                    AppUtils.showTip(CxllLlActivity.this, "员工已存在");
                else if(!pisUser.isEmpty())
                    pisUser = pisUser + "," + result.substring(5);
                else
                    pisUser =  result.substring(5);
                mEditTextUser.setText(pisUser);
                PreferenceManager.getDefaultSharedPreferences(this).edit().putString(CxllLlYGBH,pisUser).commit();
            }
        }
        if(requestCode == 0x0004 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("10010")) {
                AppUtils.showTip(CxllLlActivity.this, "请扫描现场库位。");
            }else {
                mEditTextFixnr.setText(result.substring(9));
                PreferenceManager.getDefaultSharedPreferences(this).edit().putString(CxllLlFIXNR,result.substring(9)).commit();
            }
        }
    }

    private void titleBar() {
        TextView mTextView =(TextView)findViewById(R.id.top_title_info);
        mTextView.setText("领料确认");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }
    private void initData() {
        manpahdTBeanNew = manpahdTBean =  (ProgramLlLabBean.ProgramLlBean)getIntent().getSerializableExtra("cxllDetail");
        mEditTextAppnum = (EditText) findViewById(R.id.whm_cxll_ll_appnum);
        mEditTextAppnum.setText(manpahdTBean.getAppNum().toString());

        mEditTextOnhand1 = (EditText) findViewById(R.id.whm_cxll_ll_onhand1);
        mEditTextOnhand2 = (EditText) findViewById(R.id.whm_cxll_ll_onhand2);
        //double num = manpahdTBean.getAppNum()>manpahdTBean.getOnhand1()?manpahdTBean.getOnhand1():manpahdTBean.getAppNum();
        mEditTextOnhand1.setText("0");
        mEditTextOnhand2.setText("0");

        mEditTextUser = (EditText) findViewById(R.id.whm_cxll_ll_pisuser);
        String ygbh = PreferenceManager.getDefaultSharedPreferences(this).getString(CxllLlYGBH,null);
        if(ygbh!=null){
            mEditTextUser.setText(String.valueOf(ygbh));
        }

        mEditTextFixnr = (EditText) findViewById(R.id.whm_cxll_ll_fixnr);
        String fixnr = PreferenceManager.getDefaultSharedPreferences(this).getString(CxllLlFIXNR,null);
        if(fixnr!=null){
            mEditTextFixnr.setText(String.valueOf(fixnr));
        }

        LinearLayout onhand2linearLayout = (LinearLayout) findViewById(R.id.whm_cxll_ll_onhand2_layout);
        if(manpahdTBean.getUnmsr2().isEmpty()){
            onhand2linearLayout.setVisibility(View.GONE);
        }else {
            TextView unmsr1 = (TextView)findViewById(R.id.whm_cxll_ll_onhand2_u);
            unmsr1.setText(manpahdTBean.getUnmsr2());
        }
    }
    //执行
    private void execLL(){
        String result = null;
        try {
            Map<String, String> params = new HashMap<String, String>();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Keyinuserno", mEditTextUser.getText());
            jsonObject.put("Fixnr", mEditTextFixnr.getText());
            jsonObject.put("OperationType", 2);
            jsonObject.accumulate("ProgramsInfo", new Gson().toJson(manpahdTBeanNew).toString());
            jsonObject.accumulate("InvbalInfo",new Gson().toJson(mInvbalBean).toString());
            params.put("action", "306");
            params.put("params", jsonObject.toString());
            VolleyUtil.sendStringRequestByPost(CxllLlActivity.this, HttpConfig.HTTP_MAIN_SERVER_URL, TAG, params, null, new CommHttpListener<Object>() {
                @Override
                public void onResponse(String result) {
                    super.onResponse(result);
                    if (result == null) {
                        return;
                    }
                    BaseBean resultBean = new Gson().fromJson(result, BaseBean.class);
                    if (resultBean.getCode() == 1) {
                        AppUtils.showTip(CxllLlActivity.this, "发料成功");
                        CxllLlActivity.this.setResult(RESULT_OK);
                    } else {
                        AppUtils.showTip(CxllLlActivity.this, resultBean.getMsg());
                        CxllLlActivity.this.setResult(RESULT_CANCELED);
                    }
                    finish();
                }

                @Override
                public void onCodeError(int code, String msg) {
                    AppUtils.showTip(CxllLlActivity.this, msg);
                }

                @Override
                public void onVolleyError(VolleyError volleyError) {
                    AppUtils.showTip(CxllLlActivity.this, R.string.network_error);
                }

                @Override
                public void onException(Exception e) {
                    AppUtils.showTip(CxllLlActivity.this, e.toString());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            AppUtils.showTip(CxllLlActivity.this, "执行失败，请联系IT人员");
        }
    }
}
